﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.DataContract;

using ActivitySystem.DataContract;


namespace ActivitySystem.BusinessLogic
{
    public partial class ActivityManager
    {
        public Collection<Activity> Search(string type, string title, string launcher, DateTime date, int pageIndex, int pageSize)
        {
            return controller.Search(type, title, launcher, date, pageIndex, pageSize);
        }

        public int SearchCount(string type, string title, string launcher, DateTime date)
        {
            return controller.SearchCount(type, title, launcher, date);
        }

        public void Host(Activity activity, User launcher, Collection<ActivityType> types, Collection<User> invitedUsers)
        {
            if (activity.EndTime < DateTime.Now)
            {
                throw new Exception("The activity end time is before current time");
            }

            if (launcher == null)
            {
                throw new Exception("The launcher cannot be empty");
            }

            if (types == null || types.Count == 0)
            {
                throw new Exception("The activity must has at least one type");
            }

            activity.Id = Guid.NewGuid();
            activity.LauncherId = launcher.Id;
            activity.IsApproved = true; // TODO: Approve base on system configuration.
            activity.IsActive = true;
            activity.InsertTimeStamp = DateTime.Now;
            activity.UpdateTimeStamp = DateTime.Now;

            activity.ActivityTypes.Clear();
            foreach (ActivityType type in types)
            {
                activity.ActivityTypes.Add(type.Id);
            }

            ParticipationManager participationManager = new ParticipationManager();
            TransactionHelper.ProcessTransaction(new Action(delegate
            {
                Insert(activity);

                Participation launcherParticipation = new Participation();
                launcherParticipation.Id = Guid.NewGuid();
                launcherParticipation.ActivityId = activity.Id;
                launcherParticipation.UserId = launcher.Id;
                launcherParticipation.Comments = string.Empty;
                launcherParticipation.Fellow = 0;
                launcherParticipation.IsApproved = true;
                launcherParticipation.IsActive = true;
                launcherParticipation.InsertTimeStamp = DateTime.Now;
                launcherParticipation.UpdateTimeStamp = DateTime.Now;
                participationManager.Insert(launcherParticipation);

                if (invitedUsers != null)
                {
                    foreach (User invitedUser in invitedUsers)
                    {
                        if (invitedUser == launcher)
                        {
                            continue;
                        }

                        Participation participation = new Participation();

                        participation.Id = Guid.NewGuid();
                        participation.ActivityId = activity.Id;
                        participation.UserId = invitedUser.Id;
                        participation.Comments = "Invited by launcher";
                        participation.Fellow = 0;
                        participation.IsApproved = true;
                        participation.IsActive = true;
                        participation.InsertTimeStamp = DateTime.Now;
                        participation.UpdateTimeStamp = DateTime.Now;

                        participationManager.Insert(participation);
                    }
                }
            }));
        }

        public void Dismiss(Guid userId, Guid activityId)
        {
            Activity activity = controller.Select(activityId);
            if (activity.LauncherId != userId)
            {
                throw new Exception("You are not the launcher");
            }

            controller.Delete(activity.Id);
        }
    }
}
